#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "tingfeng"
# Date: 2020/4/29

import re
import time
from util.get_by_local import GetByLocal
import time
from base.base_driver import BaseDriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from util.server import Server

class LoginPage:
    #获取登录页面所有的页面元素信息
    def __init__(self,i):
        base_driver = BaseDriver()
        self.driver = base_driver.android_driver(i)
        self.get_by_local = GetByLocal(i)
        #self.driverB = base_driver.android_driverB(i)
        #self.get_by_localB = GetByLocal(i)

    def is_element_exist(self, element):
        source = self.get_by_local.driver.page_source
        print(source)
        if element in source:
            return True
        else:
            return False

    def Save_ScreenShot(self,x):
        return self.get_by_local.driver.save_screenshot(x)

    def get_password_element(self):
        '''
        获取输入密码元素
        :return:
        '''
        time.sleep(2)
        return self.get_by_local.get_element('password','base_element')

    def get_loginButton_element(self):
        '''
        登录元素
        '''
        time.sleep(2)
        return self.get_by_local.get_element('loginButton','base_element')

    def get_web_view(self):
        time.sleep(10)
        webview = self.get_by_local.driver.contexts
        for viw in webview:
            if 'android.view.View' in viw:
                self.get_by_local.driver.switch_to.context(viw)
                break
        self.get_by_local.driver.find_element_by_link_text('C').click()
        try:
            self.get_by_local.driver.find_element_by_id('cn.com.open.mooc:id/left_icon').click()
        except Exception as e:

            self.get_by_local.driver.switch_to.context(webview[0])
            self.get_by_local.driver.find_element_by_id('cn.com.open.mooc:id/left_icon').click()
            raise e
        print(webview)


    def scroll_pageCbycard(self,):
        # 获取【推荐】、【轮胎】两个元素，将页面向左
        time.sleep(5)
        stop_element = self.get_by_local.get_element('yb_yhcard','conserve_element')
        start_element = self.get_by_local.get_element('minecrad','conserve_element')
        return self.get_by_local.driver.scroll(start_element, stop_element, 3000)

    def get_SizeC(self):
        x = self.get_by_local.driverC.get_window_size()['width']
        y = self.get_by_local.driver.get_window_size()['height']
        return (x, y)

    def Swipe_DownC(self,t):  # 屏幕向下滑动
        l = self.get_SizeC()
        x1 = int(l[0] * 0.5)  # x坐标
        y1 = int(l[1] * 0.7)  # 起始y坐标
        y2 = int(l[1] * 0.10)  # 终点y坐标
        print(x1, y1, x1, y2)
        self.get_by_local.driver.swipe(x1, y1, x1, y2, t)

    def Swipe_UpC(self,t):  # 屏幕向下滑动
        l = self.get_SizeC()
        x1 = int(l[0] * 0.5)  # x坐标
        y1 = int(l[1] * 0.15)  # 起始y坐标
        y2 = int(l[1] * 0.8)  # 终点y坐标
        print(x1, y1, x1, y2)
        self.get_by_local.driver.swipe(x1, y1, x1, y2, t)

    def Swip_LeftC(self,t):  # 向左滑  t 是时间 默认5毫秒
        l = self.get_SizeC()
        x1 = int(l[0] * 0.75)
        y1 = int(l[1] * 0.5)
        x2 = int(l[0] * 0.05)
        print(x1, y1, x2, y1)
        self.get_by_local.driver.swipe(x1, y1, x2, y1, t)

    def Swip_LeftCYQC(self,t):  # 向左滑  t 是时间 默认5毫秒
        l = self.get_SizeC()
        x1 = int(l[0] * 0.75)
        y1 = int(l[1] * 0.1586)
        x2 = int(l[0] * 0.05)
        print(x1, y1, x2, y1)
        self.get_by_local.driver.swipe(x1, y1, x2, y1, t)

    def Swip_LeftBDC(self,t):  # 向左滑  t 是时间 默认5毫秒
        l = self.get_SizeC()
        x1 = int(l[0] * 0.75)
        y1 = int(l[1] * 0.7678)
        x2 = int(l[0] * 0.05)
        print(x1, y1, x2, y1)
        self.get_by_local.driver.swipe(x1, y1, x2, y1, t)

    def Swip_RightC(self,t):  # 向左滑  t 是时间 默认5毫秒
        l = self.get_SizeC()
        x1 = int(l[0] * 0.05)
        y1 = int(l[1] * 0.5)
        x2 = int(l[0] * 0.75)
        print(x1, y1, x2, y1)
        self.get_by_local.driver.swipe(x1, y1, x2, y1, t)

    def Swip_RightCYQC(self,t):  # 向左滑  t 是时间 默认5毫秒
        l = self.get_SizeC()
        x1 = int(l[0] * 0.75)
        y1 = int(l[1] * 0.1586)
        x2 = int(l[0] * 0.05)
        print(x1, y1, x2, y1)
        self.get_by_local.driver.swipe(x1, y1, x2, y1, t)

    def get_element_textC(self):
        '''
        获取页面文本元素
        :return:
        '''
        time.sleep(2)
        return self.get_by_local.get_element('textview')

    def get_element_ViewGroupC(self):
        '''
        获取视图租元素
        '''
        time.sleep(2)
        return self.get_by_local.get_element('ViewGroup')

    def get_element_imageC(self):
        '''
        获取页面图片元素
        :return:
        '''
        time.sleep(3)
        return self.get_by_local.get_element('imageview')

    def Switch_App(self):
        '''
        切换至B端
        '''
        time.sleep(1)
        return self.get_by_local.driver.start_activity('com.youbei.store','MainActivity')

    def Switch_CApp(self):
        '''
        切换至C端
        '''
        time.sleep(1)
        return self.get_by_local.driver.start_activity('com.youbei.chefu','MainActivity')

    def get_tost_elementC(self, message):
        '''
        获取tostelement
        '''
        time.sleep(1)
        tost_element = ("xpath", "//*[contains(@text," + message + ")]")
        return WebDriverWait(self.driver, 10, 0.1).until(EC.presence_of_element_located(tost_element))

if __name__ == "__main__":
    login_page = LoginPage(0)
    # time.sleep(5)
    # login_page.get_by_local.driver.press_keycode(4)
    # time.sleep(2)
    # login_page.get_by_local.driverC.press_keycode(4)
    # time.sleep(1)
    # login_page.get_washcar_elementC().click()
    # time.sleep(1)
    # login_page.get_by_local.driverC.press_keycode(4)
    # time.sleep(1)
    # login_page.get_by_local.get_elementC('refuel')
    #login_page.get_washcard_elementC()

